﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Version2
{
    class ExportToCSV
    {
        private static bool initialized = false;
        private static System.IO.FileStream fileStream = null;
        private static System.IO.StreamWriter sWriter = null;

        static void initialize(){

            //Check for folder, and create if needed
            if (!System.IO.Directory.Exists("Social Data"))
                System.IO.Directory.CreateDirectory("Social Data");

            fileStream = new System.IO.FileStream("Social Data\\" + DateTime.Now.ToShortDateString().Replace("/", "-") + " " + DateTime.Now.Hour.ToString() + "00" + ".csv", System.IO.FileMode.OpenOrCreate);
            sWriter = new System.IO.StreamWriter(fileStream);
            sWriter.WriteLine("EntryTime,EntryID,Latitude,Longitude,Entry,User");
            initialized = true;
        }

        public static void SaveToFile(List<SocialDataPoint> data)
        {
            if (!initialized)
                initialize();

            foreach (SocialDataPoint item in data)
            {
                sWriter.WriteLine("{0},{1},{2},{3},{4},{5},{6}", item.BirthDate, item.Id, item.Lat.ToString().Replace("NaN", ""), item.Lon.ToString().Replace("NaN", ""), item.Text, item.User.Name, item.PictureUrl);
            }
        }

        public static void Dispose()
        {
            sWriter.Close();
            fileStream.Close();

            sWriter.Dispose();
            fileStream.Dispose();
        }
    }
}
